package com.example.stuwork.service.impl;

import com.example.stuwork.common.Result;
import com.example.stuwork.dao.CourseDao;
import com.example.stuwork.pojo.Course;
import com.example.stuwork.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 课程服务实现类
 */
@Service
public class CourseServiceImpl implements CourseService {
    @Autowired
    private CourseDao courseDao;
    
    @Override
    public List<Course> findAll() {
        return courseDao.findAll();
    }
    
    @Override
    public Course getById(String courseId) {
        return courseDao.getById(courseId);
    }
    
    @Override
    public Result<Course> getCourseById(String courseId) {
        try {
            Course course = courseDao.getById(courseId);
            if (course != null) {
                return Result.success("获取课程成功", course);
            } else {
                return Result.error("课程不存在");
            }
        } catch (Exception e) {
            return Result.error("获取课程失败：" + e.getMessage());
        }
    }
    
    @Override
    @Transactional
    public boolean addCourse(Course course) {
        if (course == null || course.getCourseId() == null || course.getCourseName() == null) {
            return false;
        }
        
        // 检查是否已存在相同ID或名称的课程
        Course existingById = courseDao.getById(course.getCourseId());
        Course existingByName = courseDao.getByName(course.getCourseName());
        
        if (existingById != null || existingByName != null) {
            return false;  // 已存在相同ID或名称的课程
        }
        
        return courseDao.insert(course) > 0;
    }
    
    @Override
    @Transactional
    public boolean updateCourse(Course course) {
        if (course == null || course.getCourseId() == null) {
            return false;
        }
        
        // 检查课程是否存在
        Course existing = courseDao.getById(course.getCourseId());
        if (existing == null) {
            return false;
        }
        
        // 如果更改了名称，检查名称是否已被使用
        if (course.getCourseName() != null && !course.getCourseName().equals(existing.getCourseName())) {
            Course existingByName = courseDao.getByName(course.getCourseName());
            if (existingByName != null) {
                return false;  // 名称已被使用
            }
        }
        
        return courseDao.update(course) > 0;
    }
    
    @Override
    @Transactional
    public boolean deleteCourse(String courseId) {
        if (courseId == null) {
            return false;
        }
        
        // 检查课程是否存在
        Course existing = courseDao.getById(courseId);
        if (existing == null) {
            return false;
        }
        
        // TODO: 考虑检查课程是否已关联班级，如果已关联可能需要特殊处理
        
        return courseDao.delete(courseId) > 0;
    }
    
    @Override
    public List<Course> search(String keyword) {
        return courseDao.search(keyword);
    }
}